//  Copyright (C) 2014 Xiande (Triston) Cao

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "cvsie_edge.hpp"

#include "cvsie_datatype.hpp"

using namespace cvsie;


void CEdge::UpdateLength() {
  if (first_node && second_node) {
    length = first_node->GetDistanceToNode(*second_node);
  }
}


// Gets and Sets
int CEdge::GetId() const {
  return id; 
}

void CEdge::SetId(int _id) {
  id = _id; 
}

CMeshNode * CEdge::GetFistNode() const {
  return first_node; 
}

void CEdge::SetFistNode(CMeshNode *node) { 
  first_node = node; 
  UpdateLength();
}

CMeshNode * CEdge::GetSecondNode() const {
  return second_node; 
}

void CEdge::SetSecondNode(CMeshNode *node) {
  second_node = node; 
  UpdateLength();
}

void CEdge::SetFirstAndSecondNode(CMeshNode * _first_node,
    CMeshNode * _second_node) {
  first_node  = _first_node;
  second_node = _second_node;
}

double CEdge::GetLength() const {
    return length; 
}

// ctors
CEdge::CEdge(int _id, CMeshNode * _first_node, 
    CMeshNode * _second_node, double _length) : 
  id(_id), first_node(_first_node), second_node(_second_node), length(_length) {
}

CEdge &CEdge::operator=(const CEdge &src) {
  if (this != &src) {
    id = src.id;
    first_node = src.first_node;
    second_node = src.second_node;
    length = src.length;
  }
  return *this;
}

// operators
bool CEdge::operator==(const CEdge &rhs) const {
  return ((first_node == rhs.first_node) && (second_node == rhs.second_node));
}

bool CEdge::operator<(const CEdge &rhs) const {
  if ( (first_node < rhs.first_node)  || 
   ( (first_node == rhs.first_node ) &&  (second_node < rhs.second_node) ) ) {
    return true;
  }
  return false;
}
